﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DWEIP.Web.Core;
using DWEIP.Common;
using DWEIP.Models.OA;
using DWEIP.BLL.OA;
using DWEIP.Locale;

namespace DWEIP.Web.Areas.OA.Controllers
{
    public class Nbltm001Controller : BaseController
    {
        ValidationErrors errors = new ValidationErrors();
        public OA_BltBLL m_BLL = new OA_BltBLL();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pager">分页</param>
        /// <param name="queryStr">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList(GridPager pager, string queryStr, int checkFlag = 1)
        {
            OA_BltBLL m_BLL = new OA_BltBLL();

            List<nblt001Model> list = m_BLL.GetList(ref pager, queryStr, "", true, GetUserId(), checkFlag);
            var json = new
            {
                total = pager.totalRows,
                rows = (from r in list
                        select new nblt001Model()
                        {
                            pk_id = r.pk_id,
                            function_id = r.function_id,
                            folder_id = r.folder_id,
                            doc_status = r.doc_status,
                            title = r.title,
                            content = r.content,
                            cont_format = r.cont_format,
                            cont_text = r.cont_text,
                            publish_date = r.publish_date,
                            priority = r.priority,
                            blt_type = r.blt_type,
                            folder_title = r.folder_title
                        }).ToArray()

            };

            return Json(json);
        }

        #region 创建
        [SupportFilter]
        public ActionResult Create()
        {
            List<string> bltTypeList = new List<string> { "TEST分類1", "TEST分類2" };
            ViewBag.Category = new SelectList(bltTypeList);
            return View();
        }

        [HttpPost]
        [SupportFilter]
        [ValidateInput(false)]
        public JsonResult Create(nblt001Model model)
        {
            if (model != null && ModelState.IsValid)
            {
                model.recid = ResultHelper.NewId;
                model.folder_id = 0;
                model.create_user = GetUserId();
                model.create_date = ResultHelper.NowTime;
                if (m_BLL.Create(ref errors, model))
                {
                    //if (string.IsNullOrEmpty(model.Task))
                    //{
                    //    //简单任务
                    //    JobsTools.CreateTaskJob(ref errors, model.Task, "DWEIP.Jobs.MIS.DiscussPostNewTopicsJob", model.recid, model.title);//带命名空间的任务处理类名称
                    //}
                    LogHandler.WriteServiceLog(GetUserId(), "Id:" + model.recid + ",Title:" + model.title, "成功", "创建", "信息中心");
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed), JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "Id:" + model.recid + ",Title:" + model.title + "," + ErrorCol, "失败", "创建", "信息中心");
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol), JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 修改
        [SupportFilter]
        public ActionResult Edit(string id)
        {
            int pk_id = 0;
            try
            {
                pk_id = Convert.ToInt32(id);
            }
            catch
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ":" + id), JsonRequestBehavior.AllowGet);
            }

            nblt001Model entity = m_BLL.DAL_GetById(pk_id);
            //ViewBag.Category = new SelectList(categoryBLL.GetList("0"), "Id", "Name", entity.CategoryIdParent);
            List<string> bltTypeList = new List<string> { "TEST分類1", "TEST分類2" };
            ViewBag.Category = new SelectList(bltTypeList);

            return View(entity);
        }

        [HttpPost]
        [SupportFilter]
        [ValidateInput(false)]
        public JsonResult Edit(nblt001Model model)
        {
            if (model != null && ModelState.IsValid)
            {

                if (m_BLL.Edit(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "pk_id:" + model.pk_id + ",Title:" + model.title, "成功", "修改", "信息中心");
                    return Json(JsonHandler.CreateMessage(1, Resource.EditSucceed), JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "pk_id:" + model.pk_id + ",Title:" + model.title + "," + ErrorCol, "失败", "修改", "信息中心");
                    return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ":" + ErrorCol), JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 详细
        [SupportFilter]
        public ActionResult Details(string id)
        {
            int pk_id = 0;
            try
            {
                pk_id = Convert.ToInt32(id);
            }
            catch
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ":" + id), JsonRequestBehavior.AllowGet);
            }

            nblt001Model entity = m_BLL.DAL_GetById(pk_id);
            return View(entity);
        }

        #endregion

        #region 删除
        [HttpPost]
        [SupportFilter]
        public JsonResult Delete(string id)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                int pk_id = 0;
                try
                {
                    pk_id = Convert.ToInt32(id);
                }
                catch
                {
                    return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
                }

                if (m_BLL.Delete(ref errors, pk_id))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "Id:" + id, "成功", "删除", "信息中心");
                    return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "Id" + id + "," + ErrorCol, "失败", "删除", "信息中心");
                    return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
            }
        }
        #endregion


    }
}